package me.firstapp.api.shop;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;

import me.firstapp.api.base.BaseApi;
import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.JsonPage;
import me.firstapp.common.json.ListObject;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.utils.DateUtils;
import me.firstapp.common.utils.Page;
import me.firstapp.common.utils.ResponseUtils;
import me.firstapp.module.shop.Product;
import me.firstapp.service.shop.ProductService;

@Controller
@RequestMapping(value = "/product")
@Api(value = "/product", description = "商品相关接口")
public class ProductApi extends BaseApi {

	@Autowired
	private ProductService productService;

	@RequestMapping(value = "/findProductsPage.do", method = RequestMethod.POST)
	@ApiOperation(value = "分页查询商品", notes = "分页查询商品")
	public void findProductsPage(@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "20") Integer pageSize, Long categoryId,
			Integer disable, HttpServletRequest request, HttpServletResponse response) {
		PageObject<Product> result = new PageObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Page<Product> productPage = productService.findProductPage(pageNo, pageSize, categoryId, disable,
					apiClientId);
			JsonPage<Product> jsonPage = new JsonPage<Product>();
			jsonPage.setList(productPage.getEntities());
			jsonPage.setPageNo(productPage.getPageNo());
			jsonPage.setPageSize(productPage.getPageSize());
			jsonPage.setTotalCount(productPage.getEntityCount());
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setPage(jsonPage);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/findProductById.do", method = RequestMethod.GET)
	@ApiOperation(value = "查询商品详情", notes = "查询商品详情")
	public void findProductById(Long productId, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Product> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Product product = productService.findProductById(productId, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(product);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "findProductPageByParams.do", method = RequestMethod.POST)
	@ApiOperation(value = "分页查询商品", notes = "分页查询商品")
	public void findProductPageByParams(@RequestBody Map<String, Object> searchParams,
			@RequestParam(value = "pageNo", required = true, defaultValue = "1") Integer pageNo,
			@RequestParam(value = "pageSize", required = true, defaultValue = "20") Integer pageSize, String sortParam,
			String sortType, Long startTime, Long endTime, HttpServletRequest request, HttpServletResponse response) {
		PageObject<Product> result = new PageObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			Date startDate = null;
			if (startTime != null) {
				startDate = DateUtils.getDateFromTimeStamp(String.valueOf(startTime), "yyyy-MM-dd");
			}
			Date endDate = null;
			if (endTime != null) {
				endDate = DateUtils.getDateFromTimeStamp(String.valueOf(endTime), "yyyy-MM-dd");
			}
			Page<Product> page = productService.findProductPageByParams(pageNo, pageSize, searchParams, sortParam,
					sortType, startDate, endDate, apiClientId);
			JsonPage<Product> jsonPage = new JsonPage<Product>();
			jsonPage.setList(page.getEntities());
			jsonPage.setPageNo(page.getPageNo());
			jsonPage.setPageSize(page.getPageSize());
			jsonPage.setTotalCount(page.getEntityCount());
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setPage(jsonPage);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/addProduct.do", method = RequestMethod.POST)
	@ApiOperation(value = "创建商品", notes = "创建商品")
	public void addProduct(@RequestBody Map<String, Object> body, Long categoryId, String name, String deliver,
			Double deliverFee, Integer sort, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Product> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			String listImage = (String) body.get("listImage");
			String smallImage = (String) body.get("smallImage");
			String images = (String) body.get("images");
			String summary = (String) body.get("summary");
			String detail = (String) body.get("detail");
			Product product = productService.addProduct(categoryId, listImage, smallImage, images, name, summary,
					detail, deliver, deliverFee, sort, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(product);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/updateProduct.do", method = RequestMethod.POST)
	@ApiOperation(value = "更新商品", notes = "更新商品")
	public void updateProduct(@RequestBody Map<String, Object> body, Long productId, Long categoryId, String name,
			String deliver, Double deliverFee, Integer sort, Integer disable, HttpServletRequest request,
			HttpServletResponse response) {
		SingleObject<Product> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			String listImage = (String) body.get("listImage");
			String smallImage = (String) body.get("smallImage");
			String images = (String) body.get("images");
			String summary = (String) body.get("summary");
			String detail = (String) body.get("detail");
			Product product = productService.updateProduct(productId, categoryId, listImage, smallImage, images, name,
					summary, detail, deliver, deliverFee, sort, disable, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setObject(product);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/deleteProducts.do", method = RequestMethod.POST)
	@ApiOperation(value = "删除商品", notes = "删除商品")
	public void deleteProducts(String productIds, HttpServletRequest request, HttpServletResponse response) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			productService.deleteProducts(productIds, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

	@RequestMapping(value = "/findProducts.do", method = RequestMethod.GET)
	@ApiOperation(value = "查询商品列表", notes = "查询商品列表")
	public void findProducts(Integer disable, HttpServletRequest request, HttpServletResponse response) {
		ListObject<Product> result = new ListObject<>();
		try {
			Long apiClientId = (Long) request.getAttribute("apiClientId");
			List<Product> products = productService.findProducts(disable, apiClientId);
			result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			result.setItems(products);
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		ResponseUtils.renderJson(response, result);
	}

}
